package com.mljr.example.ui.h5;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.ctakit.sdk.app.base.BaseActivity;
import com.ctakit.sdk.app.service.SimpleActionCallBack;
import com.ctakit.sdk.view.annotation.LayoutContentId;
import com.ctakit.sdk.view.annotation.ViewInject;
import com.mljr.example.R;
import com.mljr.example.base.Config;
import com.mljr.example.base.MyBaseActivity;
import com.mljr.example.service.RefreshService;
import com.mljr.example.widget.jsbridge.BridgeWebView;
import com.umeng.socialize.ShareContent;



@LayoutContentId(R.layout.activity_html5)
public class Html5Activity extends MyBaseActivity {
    @ViewInject(R.id.html5)
    private BridgeWebView html5View;
    private String url;

    private ShareContent shareContent;

    @ViewInject(R.id.bar_back)
    private View bar_back;

    //是否是打开新窗口 target=“_blank”
    private int isNewWindow = 0 ;

    @Override
    public  void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitle("加载中...");
        initBack();
        bar_back.setVisibility(View.GONE);
        url = getActivity().getIntent().getStringExtra("url");
        isNewWindow = getActivity().getIntent().getIntExtra(BridgeWebView.keyNewWindow,0);
        if (TextUtils.isEmpty(url)) {
            url = Config.Server.getBaseH5Url();
        }
        //  url = "file:///android_asset/share.html" ;

        setPageData();

    }


    @Override
    protected void onResume() {
        if (RefreshService.ifNeedRefresh(Html5Activity.class, false)) {
            setPageData();
        }
        super.onResume();
    }

    @Override
    public boolean goBack(View v,boolean isPhoneBack) {

        if(!isPhoneBack){
            if (html5View.canGoBack()) {
                html5View.goBack();
            } else {
                getActivity().finish();
            }
            return true;
        }

        return false;
    }

    @Override
    public String getPageName() {
        return "Html5Activity";
    }

    private void setPageData() {
        html5View.loadUrl(url);
        html5View.setActionTitleCallBack(new SimpleActionCallBack<String>() {
            @Override
            public void onSuccess(String title) {
                if(html5View.canGoBack()||isNewWindow==1){
                    bar_back.setVisibility(View.VISIBLE);
                }else {
                    bar_back.setVisibility(View.GONE);
                }
                ////////////
                if (!TextUtils.isEmpty(title)) {
                    if (title.length() > 10) {
                        initTitle(title.substring(0, 10) + "...");
                    } else {
                        initTitle(title);
                    }
                }
            }


        });
    }

    @Override
    public BaseActivity getActivity() {
        return Html5Activity.this;
    }

    @Override
    protected void onDestroy() {
        html5View.stopLoading();
        html5View.destroy();
        super.onDestroy();
    }
}
